﻿using System;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using Skyper.Phone.View;
using System.Windows.Input;
namespace Skyper.Phone.Controls {
	public class ContactViewControl : Control,IContactView {
		#region Fields
		private bool mouseDown;
		private TextBlock textMood;
		private TextBlock textTitle;
		private RoutedEventHandler click;
		#endregion
		#region Constructor
		public ContactViewControl() {
			this.DefaultStyleKey = typeof(ContactViewControl);

			this.Loaded += (ContactViewControl_Loaded);
			this.Unloaded += (ContactViewControl_Unloaded);
		}
		#endregion
		#region Private
		private void ContactViewControl_Loaded(object sender, RoutedEventArgs e) {
			this.MouseLeave -= (ContactViewControl_MouseLeave);
			this.MouseLeftButtonDown -= (ContactViewControl_MouseLeftButtonDown);
			this.MouseLeftButtonUp -= (ContactViewControl_MouseLeftButtonUp);
		}
		private void ContactViewControl_Unloaded(object sender, RoutedEventArgs e) {
			this.MouseLeave -= (ContactViewControl_MouseLeave);
			this.MouseLeftButtonDown -= (ContactViewControl_MouseLeftButtonDown);
			this.MouseLeftButtonUp -= (ContactViewControl_MouseLeftButtonUp);
			this.click = null;
		}

		private void ContactViewControl_MouseLeftButtonUp(object sender, MouseButtonEventArgs e) {
			if (!this.mouseDown) {
				return;
			}
			this.OnClick();
			this.mouseDown = false;
		}
		private void ContactViewControl_MouseLeftButtonDown(object sender, MouseButtonEventArgs e) {
			this.mouseDown = true;
		}
		private void ContactViewControl_MouseLeave(object sender, MouseEventArgs e) {
			this.mouseDown = false;
		}
		#endregion
		#region Protected
		protected void OnClick() {
			if (this.click == null) {
				return;
			}
			this.click(this, new RoutedEventArgs());
		}
		public sealed override void OnApplyTemplate() {
			this.textMood = base.GetTemplateChild("textMood") as TextBlock;
			this.textTitle = base.GetTemplateChild("textTitle") as TextBlock;
			base.OnApplyTemplate();
		}
		#endregion
		#region Public

		#endregion
		#region Property
		public String Text {
			get {
				if (this.textTitle == null) {
					return null;
				}
				return this.textTitle.Text;
			}
			set {
				if (this.textTitle != null) {
					this.textTitle.Text = value;
				}
			}
		}
		public String Mood {
			get {
				if (this.textMood == null) {
					return null;
				}
				return this.textMood.Text;
			}
			set {
				if (this.textMood != null) {
					this.textMood.Text = value;
				}
			}
		}
		#endregion
		#region Events
		public event RoutedEventHandler Click {
			add {
				this.click = RoutedEventHandler.Combine(click, value) as RoutedEventHandler;
			}
			remove {
				this.click = RoutedEventHandler.Remove(click, value) as RoutedEventHandler;
			}
		}
		#endregion
	}
}
